package com.zookeeper.demo1.lock;

public class LockTest {

    public static void main(String[] args) throws Exception {

        for (int i = 1; i <= 10; i++) {
            new Thread(() -> {
                DistributedLock lock = new DistributedLock();
                String path = null;
                try {
                    lock.init();
                    path = lock.getLock(0);
                    if (path != null) {
                        System.out.println(Thread.currentThread().getName() + ":获取锁成功");
                        System.out.println(Thread.currentThread().getName() + ":正在处理...");
                        Thread.sleep(2000);//模拟业务处理请求
                        System.out.println(Thread.currentThread().getName() + ":处理完成");
                    } else {
                        System.out.println(Thread.currentThread().getName() + ":获取锁失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (path != null) {
                        try {
                            lock.delete(path);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }, "Thread-" + i).start();
        }

    }
}
